package com.kundi.dooradmin.controller;

import com.kundi.dooradmin.entity.IndustryInformation;
import com.kundi.dooradmin.entity.Product;
import com.kundi.dooradmin.service.IndustryInformationService;
import com.kundi.dooradmin.util.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Author 赵恒
 * Date 2020/6/24
 */
@Controller
@RequestMapping("/industry")
public class IndustryInformationController {
    @Resource
    IndustryInformationService industryInformationService;

    @RequestMapping("/getAll")
    @ResponseBody
    public List<IndustryInformation> getAll() {
        return industryInformationService.getAll();
    }

    @RequestMapping("/getIndustryById")
    @ResponseBody
    public IndustryInformation getIndustryById(Integer id) {
        return industryInformationService.getIndustryById(id);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Map<String, String> insert(IndustryInformation industryInformation, @RequestBody MultipartFile file) throws IOException {
        industryInformation.setImage(file.getBytes());
        Integer i = industryInformationService.insert(industryInformation);
        if(i > 0) {
            return MapUtil.responseMap("添加成功", "true");
        } else {
            return MapUtil.responseMap("添加失败", "false");
        }
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(IndustryInformation industryInformation) {

        Integer i = industryInformationService.update(industryInformation);
        if(i > 0) {
            return MapUtil.responseMap("修改成功", "true");
        } else {
            return MapUtil.responseMap("修改失败", "false");
        }
    }


    @RequestMapping("/updateImage")
    @ResponseBody
    public Map<String, String> updateImage(IndustryInformation industryInformation, @RequestBody MultipartFile file) throws IOException {
        industryInformation.setImage(file.getBytes());
        Integer i = industryInformationService.update(industryInformation);
        if(i > 0) {
            return MapUtil.responseMap("修改成功，刷新后可见", "success");
        } else {
            return MapUtil.responseMap("修改失败", "success");
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, String> delete(Integer id) {
        Integer i = industryInformationService.delete(id);
        if(i > 0) {
            return MapUtil.responseMap("删除成功", "true");
        } else {
            return MapUtil.responseMap("删除失败", "false");
        }
    }

    @RequestMapping("getImage")
    public Object getImage(HttpServletRequest request, Integer id, HttpServletResponse response)  {
        IndustryInformation industryInformation = industryInformationService.getIndustryByIdWithImage(id);
        if(industryInformation == null) {
            return null;
        }
        response.setContentType("image/png");
        OutputStream output = null;
        ByteArrayInputStream in ;
        try {
            output = response.getOutputStream();
            in = new ByteArrayInputStream(industryInformation.getImage());
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                output.write(buf, 0, len);
            }
            output.flush();

        } catch (IOException e) {
            e.printStackTrace();

        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

    }

    @RequestMapping("/getNewInformation")
    @ResponseBody
    public IndustryInformation getNewInformation() {
        return industryInformationService.getNewInformation();
    }

    @RequestMapping("/getPreInfo")
    @ResponseBody
    public IndustryInformation getPreInformation(Integer id) {
        return industryInformationService.getPreInformation(id);
    }

    @RequestMapping("/getNextInfo")
    @ResponseBody
    public IndustryInformation getNextInformation(Integer id) {
        return industryInformationService.getNextInformation(id);
    }
}
